"use strict";

(function () {
    var template = function template(self) {
        return self.innerHTML;
    };

    function setSelected(self, newval) {
        self.$activeIndex = newval;
        if (self.children && self.children[newval]) {
            self.children[newval].auiAddOnceClass("active");
        }
        if (self.$lastActiveIndex > -1 && self.children[self.$lastActiveIndex]) {
            self.children[self.$lastActiveIndex].auiRemoveOnceClass("active");
        }
        self.$lastActiveIndex = newval;
    }

    document.auiDefineElement("aux-sel", {
        template: template,
        props: {
            selected: {
                observerProp: function observerProp(self) {
                    return {
                        get: function get() {
                            return self.$activeIndex;
                        },
                        set: function set(newval) {
                            setSelected(self, newval);
                        }
                    };
                },
                afterCreated: function afterCreated(self, newval) {
                    self.$lastActiveIndex = -1;
                    setSelected(self, newval);
                }
            }
        },
        created: function created(self) {},
        attached: function attached(self) {}
    });
})();